package com.jdjc.auth.application.controller;

import com.google.common.base.Preconditions;
import com.jdjc.auth.application.convert.AuthRolePermissionDTOConverter;
import com.jdjc.auth.application.dto.AuthRolePermissionDTO;
import com.jdjc.auth.entity.Result;
import com.jdjc.auth.domin.entity.AuthRolePermissionBO;
import com.jdjc.auth.domin.service.AuthRolePermissionDominService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class RolePermissionController {
    @Resource
    private AuthRolePermissionDominService authRolePermissionDominService;
    /**
     * 新增角色权限关联关系
     * @param authRolePermissionDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController：add:dto:{}", authRolePermissionDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()), "权限id集合不能为空");
            //自定义类型装换
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            if (log.isInfoEnabled()) {
                log.info(" RolePermissionController：add:bo:{}", authRolePermissionBO);
            }
            //注册
            Boolean result = authRolePermissionDominService.add(authRolePermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RolePermissionController:add:error:{}", e.getMessage(), e);
            return Result.fail("新增角色权限关联失败");
        }

    }
}
